use actix_web::web::Data;
use actix_web::{App, HttpServer};

mod config;
mod controller;
mod route;

use config::app_config::AppConfig;
use log::info;
use sqlx::mysql::MySqlPoolOptions;

#[warn(non_snake_case)]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
    log4rs::init_file("config/log4rs.yaml", Default::default()).unwrap();

    info!("{}", "startup ...");

    let app_config = AppConfig::new("config/config.ini");
    info!(
        "name: {}, datasource: {}",
        app_config.name.to_string(),
        app_config.datasource
    );

    let pool = MySqlPoolOptions::new()
        .max_connections(20)
        .connect(&app_config.datasource)
        .await
        .unwrap();

    HttpServer::new(move || {
        App::new()
            .app_data(Data::new(pool.clone()))
            //.service(home_page)
            .configure(route::config_route)
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}
